﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using SharpMap.Wpf.Rendering;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;

namespace SharpMap.Presentation.Wpf.Converters
{
    /// <summary>
    /// Convertes from SharpMap.Styles.StyleColor to System.Windows.Media.Color
    /// or System.Windows.Media.SolidColorBrush
    /// </summary>
    public class BrushConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is SharpMap.Styles.StyleBrush)
            {
                SharpMap.Styles.StyleBrush sourceBrush = (SharpMap.Styles.StyleBrush)value;
                if (targetType.IsAssignableFrom(typeof(System.Windows.Media.Color)))
                {
                    return ViewConverter.Convert(sourceBrush);
                }
                if (targetType.IsAssignableFrom(typeof(System.Windows.Media.Brush)))
                {
                    return ViewConverter.Convert(sourceBrush);
                }

                if (targetType == typeof(string))
                {
                    return ViewConverter.Convert(sourceBrush).ToString();
                }
            }

            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is System.Windows.Media.Brush)
            {
                System.Windows.Media.Brush sourceColor = (System.Windows.Media.Brush)value;
                if (targetType.IsAssignableFrom(typeof(SharpMap.Styles.StyleBrush)))
                {
                    return ViewConverter.Convert(sourceColor);
                }
            }
            if (value is string)
            {
                try
                {
                    System.Windows.Media.Brush brush = (System.Windows.Media.Brush)_wpfBrushConverter.ConvertFromString(value as string);

                    return ViewConverter.Convert(brush);
                }
                catch (InvalidCastException e)
                {
                    Debug.Write(string.Format("Error converting back from {0} to {1}\n{2}"
                        ,value.GetType().ToString(),"SharpMap.Styles.StyleBrush",e.Message));
                                    
                    return DependencyProperty.UnsetValue;
                }
            }
            return DependencyProperty.UnsetValue;
        }

        #endregion
        static System.Windows.Media.BrushConverter _wpfBrushConverter = new System.Windows.Media.BrushConverter();

        static BrushConverter _instance = null;
        public static BrushConverter Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new BrushConverter();
                }
                return _instance;
            }

        }
    }
}
